// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },

        m_nPlayerScore : 100,
        m_strPlayerName : "",
        m_nIconIndex : 0,
        m_nRank : 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function ()
    {
        // var sprite = this.node.getComponent(cc.Sprite);
        // sprite.spriteFrame.setTexture("res/raw-assets/Texture/myResultCell.png");

        var mySprite = this.node.getChildByName("myResultCell");
        mySprite.active = false;
    },

    setScore : function (nScore)
    {
        this.m_nPlayerScore = nScore;

        var playerScoreLabel = this.node.getChildByName('scoreLabel');
        playerScoreLabel.getComponent(cc.Label).string = nScore.toString();

        var progressBarNode = this.node.getChildByName('progressBar');
        var progressBarSprite = progressBarNode.getComponent(cc.Sprite);
        progressBarSprite.fillRange = this.m_nPlayerScore > 0 ? this.m_nPlayerScore / 100 : 0;
    },

    setName : function (strName)
    {
        this.m_strPlayerName = strName;

        var playerName = this.node.getChildByName('nameLabel');
        playerName.getComponent(cc.Label).string = strName;
    },

    setRank : function (nRank)
    {
        this.m_nRank = nRank;

        var playerRankLabel = this.node.getChildByName('rankLabel');
        playerRankLabel.getComponent(cc.Label).string = nRank.toString();
    },

    setIcon : function (nIcon)
    {
        this.m_nIconIndex = nIcon;

        var iconBaseNode = this.node.getChildByName('iconBase');
        var iconImageNode = iconBaseNode.getChildByName('iconImage');

        // iconImageNode.setScale(0.87);
        // iconImageNode.setPosition(cc.p(0, -0.5));

        var imageScripte = iconImageNode.getComponent('IconImageScripte');
        imageScripte.setIcon(nIcon);
    },

    setMyCell : function ()
    {
        // var sprite = this.node.getComponent(cc.Sprite);

        // case1 failed
        // var texture = cc.textureCache.addImage("res/raw-assets/Texture/myResultCell.png");
        // sprite.spriteFrame.setTexture(texture);

        // case2 failed
        // sprite.spriteFrame.clearTexture();
        // sprite.spriteFrame.setTexture("res/raw-assets/Texture/myResultCell.png");

        var mySprite = this.node.getChildByName("myResultCell");
        mySprite.active = true;
    },

    start () {

    },

    // update (dt) {},
});
